package com.lyy.java_chatroom.config;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.lyy.java_chatroom.api.WebSocketApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * 用于配置 WebSocket 的处理程序和拦截器
 * 通过 HttpSessionHandshakeInterceptor将HttpSession中的属性传递到WebSocketSession中
 * 这样就可以在 WebSocket代码中 WebSocketSession中拿到 HttpSession中的 attribute
 */

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private WebSocketApi webSocketApi;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketApi,"/wsmessage")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
